package com.study.service.communication;

import com.alibaba.fastjson.JSON;
import com.study.constant.CommonConstant;
import com.study.vo.JwtToken;
import com.study.vo.UsernameAndPassword;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 使用 ribbon 实现微服务通信
 */

@Slf4j
@Service
public class UseRibbonService {

    @Autowired
    private RestTemplate restTemplate;

    public JwtToken getTokenFromAuthorityServiceByRibbon( UsernameAndPassword usernameAndPassword){
        //url ip 换成了服务名称
        String requestUrl = String.format("http://%s/" +
                "ecommerce-authority-center/authority/getToken",
                CommonConstant.AUTHORITY_CENTER_SERVICE_ID
        );

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);

        return restTemplate.postForObject(
                requestUrl,
                new HttpEntity<>(JSON.toJSONString(usernameAndPassword),httpHeaders),
                JwtToken.class
        );





    }

}
